package com.qi.demo01.Lock8;

import java.util.concurrent.TimeUnit;

/**
 * @author zjq
 * @date 2020/3/25 19:59
 */

public class Test1 {
    public static void main(String[] args) throws InterruptedException {
        Phone1 phone1 = new Phone1();
        Phone1 phone2 = new Phone1();//两个对象，两个锁。按照时间来

        new Thread(() -> {
            try {
                phone1.sendSms();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "A").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(() -> {
            phone2.call();
        }, "B").start();
    }
}

class Phone1 {
    //Synchronized 锁的对象是方法的调用者
    public synchronized void sendSms() throws InterruptedException {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("send");
    }

    public synchronized void call() {
        System.out.println("call");
    }

    //这里没有锁，不是同步方法，不收锁的影响
    public void Hello() {
        System.out.println("hello");
    }
}

